package com.stylefeng.guns.rest.modular.App.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.rest.modular.App.dao.LineTimeSlotMapper;
import com.stylefeng.guns.rest.modular.App.model.LineTimeSlot;
import com.stylefeng.guns.rest.modular.App.service.ILineTimeSlotService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 列车时刻表 Service 实现
 *
 * @author 张亚林 @Date 2018-11-16 18:39
 */
@Service
public class LineTimeSlotServiceImpl extends ServiceImpl<LineTimeSlotMapper, LineTimeSlot> implements ILineTimeSlotService {

  @Resource private LineTimeSlotMapper lineTimeSlotMapper;

  /**
   * 删除列车时刻表
   *
   * @param lineTimeSlot
   * @return
   */
  @Transactional
  public int deleteLineTimeSlot(LineTimeSlot lineTimeSlot, String token) throws Exception {
    if (lineTimeSlot == null) {
      throw new RuntimeException("参数不全！");
    }
    return this.lineTimeSlotMapper.deleteLineTimeSlot(lineTimeSlot);
  }
  /**
   * 批量删除列车时刻表
   *
   * @param ids 主键ID数组
   * @return
   */
  @Transactional
  public int deleteBatchLineTimeSlot(String[] ids, String token) throws Exception {
    return this.lineTimeSlotMapper.deleteBatchLineTimeSlot(ids);
  }
  /**
   * 查询列车时刻表列表
   *
   * @param lineTimeSlot 列车时刻表对象
   * @return
   */
  public List<LineTimeSlot> selectListLineTimeSlot(LineTimeSlot lineTimeSlot, String token)
      throws Exception {
    return this.lineTimeSlotMapper.selectListLineTimeSlot(lineTimeSlot);
  }
  /**
   * 分页查询列车时刻表列表
   *
   * @param pager 分页对象
   * @return
   */
  public Map pageListLineTimeSlot(Pager pager, String token) throws Exception {
    Page page = new Page(pager.getPageNumber(), pager.getPageSize());
    page.setRecords(lineTimeSlotMapper.selectListLineTimeSlot(page, (LineTimeSlot) pager.getObj()));
    List<LineTimeSlot> list = page.getRecords();
    Map mapDate = new HashMap();
    mapDate.put("rows", list);
    mapDate.put("total", page.getTotal());
    mapDate.put("page", page.getCurrent());
    mapDate.put("pageCount", page.getPages());
    return mapDate;
  }
  /**
   * 同一个事务 互斥锁查询
   *
   * @param lineTimeSlot 列车时刻表对象
   * @return
   */
  public List<LineTimeSlot> selectListLineTimeSlotForUpdate(LineTimeSlot lineTimeSlot)
      throws Exception {
    return this.lineTimeSlotMapper.selectListLineTimeSlotForUpdate(lineTimeSlot);
  }
  /**
   * 通过主键id查询的列车时刻表列表
   *
   * @param id 主键id
   * @return
   */
  public LineTimeSlot findLineTimeSlotById(String id, String token) throws Exception {
    return this.lineTimeSlotMapper.findLineTimeSlotById(id);
  }

  /**
   * 新增列车时刻表数据
   *
   * @param lineTimeSlot 列车时刻表对象
   * @return
   */
  @Transactional
  public int insertLineTimeSlot(LineTimeSlot lineTimeSlot, String token) throws Exception {
    if (lineTimeSlot != null
        && (lineTimeSlot.getLtsId() == null || lineTimeSlot.getLtsId().trim().length() < 10)) {
      lineTimeSlot.setOlId(IdWorker.get32UUID());
    }
    return this.lineTimeSlotMapper.insertLineTimeSlot(lineTimeSlot);
  }

  /**
   * 更新列车时刻表数据
   *
   * @param lineTimeSlot 列车时刻表对象
   * @return
   */
  @Transactional
  public int updateLineTimeSlot(LineTimeSlot lineTimeSlot, String token) throws Exception {
    return this.lineTimeSlotMapper.updateLineTimeSlot(lineTimeSlot);
  }
  /**
   * 查询列车时刻表返回key-Value属性
   *
   * @param lineTimeSlot 系统操作对象
   * @return
   */
  public List<CommonKeyValue> optionsLineTimeSlot(LineTimeSlot lineTimeSlot, String token)
      throws Exception {
    List<LineTimeSlot> listLineTimeSlot = this.lineTimeSlotMapper.selectListLineTimeSlot(lineTimeSlot);
    List<CommonKeyValue> listOptions = new ArrayList<CommonKeyValue>();
    for (LineTimeSlot item : listLineTimeSlot) {
      CommonKeyValue opts = new CommonKeyValue();
      opts.setId("" + item.getLtsId());
      opts.setValue(item.getOlId());
      listOptions.add(opts);
    }
    return listOptions;
  }
}